/**
 * <p>Title: Ammo</p>
 * <p>Description: Ammo is basicaly a set of bullets, but in this very case this class
 * should be called AmmoSpent, cause this Ammo holds only the bullets already shot ;)</p>
 * @author Guilherme Mauro Germoglio
 */

import java.util.*;

public class Ammo {

  // atributes
  private List bullets;
  private boolean paused;

  //methods

  /**
   * Ammo's constructor. It receives nothing as you can see.
   */
  public Ammo() {
    this.bullets = new LinkedList();
    this.paused = false;
  }

  /**
   * It adds a bullet to ammo. It happens when a bullet was shot.
   * @param bullet The bullet that was shot.
   */
  public synchronized void addBullet(Bullet bullet) {
    bullets.add(bullet);
  }

  /**
   * The only way to get all the bullets that are flying through the outer space.
   * @return An array of bullets
   */
  public Bullet[] getBullets() {
    int bulletsArrayLength = bullets.size();
    Bullet[] bulletsArray = new Bullet[bulletsArrayLength];
    for (int k = 0; k < bulletsArrayLength; k++) {
      bulletsArray[k] = (Bullet) bullets.get(k);
    }
    return bulletsArray;
  }

  /**
   * Checks if there's any bullet
   * @return True if so, false case not
   */
  public boolean hasBullets() {
    if (bullets.size() > 0)
      return true;
    else
      return false;
  }

  /**
   * Checks if there's any bullet that could be disposed forever and ever. (i.e. case a bullet
   * goes beyond the window's limits.
   */
  public synchronized void removeBullets() {
    Iterator i = bullets.iterator();
    while (i.hasNext()) {
      Bullet bullet = (Bullet) i.next();
      if (bullet.isOutOfBounds() || bullet.hasHitSomething())
        i.remove();
    }
  }

  /**
   * Pause all the bullets' threads
   */
  public synchronized void pauseThreads() {
    this.paused = true;
    Iterator i = bullets.iterator();
    while (i.hasNext()) {
      ( (Bullet) i.next()).setPause(true);
    }
  }

  /**
   * Depause all the bullets' threads
   */
  public synchronized void depauseThreads() {
    this.paused = false;
    Iterator i = bullets.iterator();
    while (i.hasNext()) {
      ( (Bullet) i.next()).setPause(false);
    }
  }

  /**
   * Returns if all the bullets' threads are paused or not
   * @return If the bullets are paused or not
   */
  public boolean isPaused() {
    return paused;
  }
}